﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using W.Carrier.Domain;
using W.Carrier.Domain.PageDTO;
using W.Carrier.Read.Api.Application.Command;
using W.Carrier.Read.Api.Server;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace W.Carrier.Read.Api.Controllers
{
    /// <summary>
    /// 承运商读控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class CarrierController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<CarrierController> _logger;
        private readonly httpshow _httpshow;
        public CarrierController(IMediator mediator, ILogger<CarrierController> logger, httpshow httpshow)
        {
            _mediator = mediator;
            _logger = logger;
            _httpshow = httpshow;
        }
        /// <summary>
        /// 承运商显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageInfo<CarrierInfo>>> GetAllCarrierAsync([FromQuery]CarrierShowCommand command)
        {
            ApiResult<PageInfo<CarrierInfo>> result =new ApiResult<PageInfo<CarrierInfo>>();
            try
            {
                _logger.LogInformation("调用承运商显示方法");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用承运商显示方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 根据承运商Id查询承运商信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<CarrierInfo>> GetModelCarrierAsync([FromQuery] CarrierModelCommand command)
        {
            ApiResult<CarrierInfo> result = new ApiResult<CarrierInfo>();
            try
            {
                _logger.LogInformation("调用根据承运商Id查询承运商信息方法");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用根据承运商Id查询承运商信息方法时出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 导出承运商
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportExcel([FromQuery]CarrierShowCommand command)
        {
            var list=await _mediator.Send(command); 

            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("承运商信息");

            IRow rowl = sheet.CreateRow(0);
            rowl.CreateCell(0).SetCellValue("承运商编号");
            rowl.CreateCell(1).SetCellValue("承运商名称");
            rowl.CreateCell(2).SetCellValue("备注");

            
            int index = 1;
            foreach (var item in list.List)
            {
                rowl = sheet.CreateRow(index);
                rowl.CreateCell(0).SetCellValue(item.CarrierNo);
                rowl.CreateCell(1).SetCellValue(item.CarrierName);
                rowl.CreateCell(2).SetCellValue(item.CarrierRemarks);
                index++;
            }
            byte[] bytes;

            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                bytes = ms.ToArray();
            }
            string mapPath = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes,mapPath,"承运商信息.xlsx");
        }




    }
}
